package org.jeecg.modules.demo.analysis.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.analysis.entity.ZnzfAnalysis;
import org.jeecg.modules.demo.applyinfo.entity.ZnzfApply;
import org.jeecg.modules.demo.projectroom.entity.ZnzfProject;
import org.jeecg.modules.demo.projectroom.entity.ZnzfRoom;
import org.jeecg.modules.demo.projectroom.service.IZnzfProjectService;
import org.jeecg.modules.demo.projectroom.service.IZnzfRoomService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.security.provider.certpath.OCSP;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 数据分析
 * @author: scott
 * @date: 2022年03月31日 16:02
 */

@Api(tags="数据分析")
@RestController
@RequestMapping("/analysis")
@Slf4j
public class ZnzfAnalysisController {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IZnzfProjectService znzfProjectService;

    @Autowired
    private IZnzfRoomService znzfRoomService;

    @ApiOperation(value = "数据分析-测试",notes = "测试")
    @GetMapping(value = "/gettest")
    public Result<SysUser> gettest(HttpServletRequest request){
        String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
        String name = JwtUtil.getUsername(token);
        SysUser sysUser = sysUserService.getUserByName(name);
        return Result.OK(sysUser);
    }

    /**
     * 获取床位数据
     * @param request
     * @return
     */
    @ApiOperation(value = "数据分析-获取床位数据",notes = "获取床位数据")
    @GetMapping(value = "/getBed")
    public Result<ZnzfAnalysis> getBed(HttpServletRequest request){
        String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
        String name = JwtUtil.getUsername(token);
        SysUser sysUser = sysUserService.getUserByName(name);
        ZnzfAnalysis znzfAnalysis = new ZnzfAnalysis();
        //TODO 权限控制
        List<ZnzfProject> znzfProjectList = znzfProjectService.list();
        if(oConvertUtils.isEmpty(znzfProjectList))
            return Result.error("不存在相关床位信息");
        int cnt = 0;
        for(ZnzfProject znzfProject : znzfProjectList){
            if(oConvertUtils.isEmpty(znzfProject.getCity()))
                continue;
            List<String> cityList = new ArrayList<>();
            List<Integer> hasLive = new ArrayList<>();
            List<Integer> emptyLive = new ArrayList<>();
            List<Integer> waitLive = new ArrayList<>();
            if(oConvertUtils.isNotEmpty(znzfAnalysis.getCityList())){
                cityList = znzfAnalysis.getCityList();
                hasLive = znzfAnalysis.getHasLive();
                emptyLive = znzfAnalysis.getEmptyLive();
                waitLive = znzfAnalysis.getWaitLive();
            }

            List<ZnzfRoom> znzfRoomList = znzfRoomService.searchsByProjectId(znzfProject.getId());
            Integer has = 0;
            Integer empty = 0;
            Integer wait = 0;
            if(oConvertUtils.isNotEmpty(znzfRoomList)){
                for(ZnzfRoom znzfRoom : znzfRoomList){
                    has = has + znzfRoom.getLivedNum();
                    empty = empty + znzfRoom.getBedNum() - znzfRoom.getLivedNum();
                }
            }
            String turnId = znzfProject.getTurnId();
            String[] turnIds = new String[0];
            if (oConvertUtils.isNotEmpty(turnId)) {
                turnIds = turnId.split(";");
            }
            wait = turnIds.length;
            int tmp = 0;
            for(tmp = 0; tmp < cnt; tmp++){
                if(Objects.equals(cityList.get(tmp), znzfProject.getCity())){
                    hasLive.set(tmp, hasLive.get(tmp) + has);
                    emptyLive.set(tmp, emptyLive.get(tmp) + empty);
                    waitLive.set(tmp, waitLive.get(tmp) + wait);
                    break;
                }
            }
            if(tmp == cnt){
                cityList.add(znzfProject.getCity());
                hasLive.add(has);
                emptyLive.add(empty);
                waitLive.add(wait);
                cnt++;
            }
            znzfAnalysis.setCityList(cityList);
            znzfAnalysis.setHasLive(hasLive);
            znzfAnalysis.setEmptyLive(emptyLive);
            znzfAnalysis.setWaitLive(waitLive);
        }
        return Result.OK(znzfAnalysis);
    }
}
